package com.quanyan.user.service.impl;

import com.quanyan.common.constants.Constants;
import com.quanyan.user.mapper.TbMedalMapper;
import com.quanyan.user.mapper.TbUserMedalRelationMapper;
import com.quanyan.user.mapper.UserMedalExtendMapper;
import com.quanyan.user.model.db.*;
import com.quanyan.user.model.vo.response.MedalRule;
import com.quanyan.user.model.vo.response.RespUserMedal;
import com.quanyan.user.model.vo.response.RespUserMedalRule;
import com.quanyan.user.service.UserMedalService;
import com.quanyan.user.utils.ThreadPoolSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.ExecutorService;

/**
 * Created by zhang on 16/7/12.
 */
@Service("userMedalService")
public class UserMedalServiceImpl implements UserMedalService {

    @Autowired
    private TbUserMedalRelationMapper userMedalRelationMapper;
    @Autowired
    private UserMedalExtendMapper userMedalExtendMapper;
    @Autowired
    private TbMedalMapper medalMapper;


    /**
     * 用户勋章列表
     * @param currentUid
     * @param uid
     * @param type
     * @return
     */
    @Override
    public List<RespUserMedal> list(int currentUid, int uid, int type, int categoryId) {

        Map map = new HashMap<>();
        map.put("uid",currentUid);
        map.put("categoryId",categoryId);
        map.put("type",type);

        List<RespUserMedal> respUserMedalList;
        //先判断该用户是否存在勋章
        List<TbUserMedalRelation> userMedalRelationList = this.getUserMedalRelationList(currentUid);
        if (userMedalRelationList.isEmpty()) {
            //先初始化用户勋章
            this.initUserMedalList(currentUid);
            respUserMedalList = this.userMedalExtendMapper.getUserMedalList(map);
        } else {
            respUserMedalList = this.userMedalExtendMapper.getUserMedalList(map);
        }
        return respUserMedalList;
    }

    /**
     * 获取勋章详情
     * @param uid
     * @param medalId
     * @return
     */
    @Override
    public RespUserMedalRule get(int uid, int medalId) {
        RespUserMedalRule clubMedalRule = new RespUserMedalRule();
        List<MedalRule> ruleList = userMedalExtendMapper.listMedalRule(medalId);
        TbMedal medal = medalMapper.selectByPrimaryKey(medalId);
        if(null != medal){
            clubMedalRule.setRuleList(ruleList);
            clubMedalRule.setMedalId(medal.getId());
            clubMedalRule.setIcon(medal.getIcon());
            clubMedalRule.setName(medal.getName());
        }
        return clubMedalRule;
    }

    /**
     * 获取用户勋章关联信息
     * @param uid
     * @return
     */
    @Override
    public List<TbUserMedalRelation> getUserMedalRelationList(int uid){
        TbUserMedalRelationExample example = new TbUserMedalRelationExample();
        TbUserMedalRelationExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo(Constants.IS_DELETE_FALSE);
        criteria.andUidEqualTo(uid);
        List<TbUserMedalRelation> list = this.userMedalRelationMapper.selectByExample(example);
        return list;
    }

    /**
     * 获取勋章列表
     * @param medalType
     * @return
     */
    @Override
    public List<TbMedal> getMedalList(Integer medalType){
        TbMedalExample example = new TbMedalExample();
        example.createCriteria().andMedalTypeEqualTo(medalType.byteValue()).andIsDeleteEqualTo(false);
        List<TbMedal> medalList = this.medalMapper.selectByExample(example);
        return medalList;
    }

    /**
     * 插入用户勋章信息
     * @param userMedalRelation
     * @return
     */
    @Override
    public int insertUserMedalRelation(TbUserMedalRelation userMedalRelation){
        int n = this.userMedalRelationMapper.insertSelective(userMedalRelation);
        return n;
    }

    /**
     * 初始化用户勋章
     * @param uid
     * @return
     */
    @Override
    public int initUserMedalList(Integer uid) {
        List<TbMedal> medalList = this.getMedalList(Constants.SystemMedalType.USER_MEDAL_TYPE);
        TbUserMedalRelation userMedalRelation;
        List<TbUserMedalRelation> userMedalRelationList = new ArrayList<>();
        for (TbMedal medal : medalList) {
            userMedalRelation = new TbUserMedalRelation();
            userMedalRelation.setUid(uid);
            userMedalRelation.setMedalStartLevel((byte)0);
            userMedalRelation.setMedalId(medal.getId());
            userMedalRelation.setGoalValue(medal.getMaxStandardValue());
            userMedalRelation.setMedalGetDate(new Date());
            userMedalRelation.setOwnValue(0);

            userMedalRelationList.add(userMedalRelation);
        }
        int n = this.userMedalExtendMapper.batchInsertUserMedalRelation(userMedalRelationList);
        return n;
    }

    /**
     * 获取已获得勋章总数
     * @param uid
     * @return
     */
    @Override
    public int getUserObtainMedalCount(Integer uid) {
        TbUserMedalRelationExample example = new TbUserMedalRelationExample();
        TbUserMedalRelationExample.Criteria criteria = example.createCriteria();
        criteria.andUidEqualTo(uid);
        int n = userMedalRelationMapper.countByExample(example);
        if (n>0) {
            criteria.andMedalStartLevelGreaterThan((byte)0);
            int k = userMedalRelationMapper.countByExample(example);
            return k;
        }else{
            this.initUserMedalList(uid);
        }
        return 0;
    }

}
